<?php
/*用户模块*/
namespace YMApi\Controller;
use Think\Controller;
class UsersController extends AppController {
   
	public function _initialize(){
		parent::_initialize();
	}
	
	
	
	/*登录*/
    public function login(){
    	$name=I('post.name');
    	if(empty($name)){
    		returnApiError('用户名不能为空');
    	}
        $password=I('post.password');
       if(empty($password)){
    		returnApiError('密码不能为空');
    	}
        $admin_user=M('admin_user');
        $ec_salt=$admin_user->field('ec_salt')->where(array('user_name'=>$name))->find(); 
        if(!empty($ec_salt['ec_salt'])){
        	$password=md5(md5($password).$ec_salt['ec_salt']);
        }else{
        	$password=md5($password);
        }

        $row=$admin_user->field('user_id,user_name,email,add_time')->where(array('user_name'=>$name,'password'=>$password))->find();
        if($row){
        	$warehouse_personnel=M('warehouse_personnel');
        	$wprow=$warehouse_personnel->field('position,wid,longitude,latitude,status')->where(array('uid'=>$row['user_id'],'position'=>2))->find();
        	if($wprow){
	        	$row['position']=$wprow['position'];
	        	$row['wid']=$wprow['wid'];
	        	$row['longitude']=$wprow['longitude'];
	        	$row['latitude']=$wprow['latitude'];
	        	$row['status']=$wprow['status'];
	            $users=array('user'=>$row);
	            $ip=get_client_ip();
	            //记录登录日志
	            $id=$admin_user->where(array('user_id'=>$row['user_id']))->save(array('last_login'=>gmtime(),'last_ip'=>$ip));
	            if($id!=false){
	            	$data=array(
	            	   'log_time'=>gmtime(),
	            	   'user_id'=>$row['user_id'],
	            	   'log_info'=>'配送员APP端：登录',
	            	   'ip_address'=>$ip
	            	);
	            	M('admin_log')->add($data);
	            }
	        	returnApiSuccess($users,'登录成功');
        	}else{
        		returnApiError('该用户没有权限登录');
        	}
        }else{
        	returnApiError('登录失败');
        }
    }
    
    
    /*
     * 修改收货地址时，将地址转换成经纬度并对应入库
     * */
    public function updateaddress(){
    	$user_id=I('post.user_id','intval');
    	$address_id=I('post.address_id','intval');
    	if(!isset($user_id) || !isset($address_id)){
    		returnApiError('参数错误');
    	}
    	$user_address_mod=M('user_address');
    	$where['user_id']=$user_id;
    	$where['address_id']=$address_id;
    	$list=$user_address_mod->where($where)->find();
    	if(count($list)>0){
    		$country_name=$this->get_region_name($list['country']);
        	$province_name=$this->get_region_name($list['province']);
        	$city_name=$this->get_region_name($list['city']);
        	$district_name=$this->get_region_name($list['district']);
        	$address=$country_name.$province_name.$city_name.$district_name.$list['address'];
    		$lnglat=get_lng_lat($address);
    		if($lnglat){
    			$longitude=$lnglat['lng'];
    			$latitude=$lnglat['lat'];
    		}else{
    			$longitude='';
    			$latitude='';
    		}
    		$data['longitude']=$longitude;
    		$data['latitude']=$latitude;
    		$id=$user_address_mod->where($where)->save($data);
    		if($id!=false){
    			returnApiSuccess(array('id'=>$id),'修改收货地址时,转换经纬度成功');
    		}else{
    			returnApiError('修改收货地址时,转换经纬度失败');
    		}
    	}else{
    		returnApiError('查无此收货地址');
    	}
    }
    
    
    /*
     * 我的收货地址列表
     * */
    public function my_address_list(){
    	parent::check_uid ();
        $uid=$this->uid;
        $page=I('post.page','intval');
        $list=D('Useraddress')->get_address_list($uid,$page);
        if($list){
            returnApiSuccess($list);
        }else{
            returnApiError('没有收货地址');
        }
    }
    
    /*
     * 新增我的收货地址
     * */
    public function my_address_add(){
        parent::check_uid ();
       $uid=$this->uid;
       $consignee=I('post.consignee');
       $mobile=I('post.mobile');
       $province=I('post.province');
       $city=I('post.city');
       $district=I('post.district');
       $address=I('post.address');
       $address_name = I ( 'post.address_name' );
       $sign_building = I ( 'post.sign_building' );
       $sex=I('post.sex');	//1 男  2女
       $address_tag=I('post.address_tag');	//1 家  2公司
       if(!$consignee || !$mobile || !$province || !$city || !$district || !$address){
       	    returnApiError('请完善收货地址信息');
       }
       $address = str_replace(array($province,$city,$district), array('','',''), $address);
		if(!strstr($address,$sign_building)){
			$address .= $sign_building;
		}

        $province_id = D('Useraddress')->get_address_id($province, 1);
        $city_id = D('Useraddress')->get_address_id($city, 2);
        $district_id = D('Useraddress')->get_address_id($district, 3);

        if(!$province_id || !$city_id || !$district_id){
            returnApiError('所选地址不在服务范围内，请重新选择');
        }
        $data = array(
            'user_id' => $uid,
            'consignee' => $consignee,
            'country' => 1,
            'province' => $province_id,
            'city' => $city_id,
            'district' => $district_id,
            'address' => $address,
            'address_name' => $address_name,
            'sign_building' => $sign_building,
            'sex' => $sex,
            'address_tag' => $address_tag,
            'mobile' => $mobile
        );


        if(strstr($address,"公交"))
        {
            $address = $province . $city . $district . $sign_building;
        }else{
            $address = $province . $city . $district . $address;
        }

        $lnglat = get_lng_lat($address);
        if ($lnglat) {
            $longitude = $lnglat['lng'];
            $latitude = $lnglat['lat'];
        } else {
            $longitude = '';
            $latitude = '';
        }
        $data['longitude'] = $longitude;
        $data['latitude'] = $latitude;
        $id = D('Useraddress')->add_address($data);
        if ($id) {

            //添加到用户行为
            $param = array('insertOne', $uid, $id, I('wid') , 'add_address', 1, 1);
            \Think\Hook::listen('receng', $param);

            returnApiSuccess(array('id' => $id), '新增收货地址成功');
        } else {
            returnApiError('新增收货地址失败');
        }
    }
    
    /*
     * 修改我的收货地址
     * */
    
    public function my_address_edit() {
    	parent::check_uid ();
		$uid = $this->uid;
		$address_id = I ( 'post.address_id', 'intval' );
		$consignee = I ( 'post.consignee' );
		$mobile = I ( 'post.mobile' );
		$province = I ( 'post.province' );
		$city = I ( 'post.city');
		$district = I ( 'post.district' );
		$address = I ( 'post.address' );
		$address_name = I ( 'post.address_name' );
		$sign_building = I ( 'post.sign_building' );
        $sex=I('post.sex');	//1 男  2女
        $address_tag=I('post.address_tag');	//1 家  2公司
		if (!$address_id  || !$consignee || !$mobile || !$province || !$city || !$district || !$address) {
			returnApiError ( '请完善收货地址信息' );
		}
		$where ['user_id'] = $uid;
		$where ['address_id'] = $address_id;
		$useraddress = D ( 'Useraddress' );
		
		$address_info = $useraddress->where ( $where )->find ();
		
		$address = str_replace(array($province,$city,$district), array('','',''), $address);
		if ($address_info) {
			$data = array (
					'user_id' => $uid,
					'address_id' => $address_id,
					'consignee' => $consignee,
					'country' => 1,
					'province' => D ( 'Useraddress' )->get_address_id ( $province, 1 ),
					'city' => D ( 'Useraddress' )->get_address_id ( $city, 2 ),
					'district' => D ( 'Useraddress' )->get_address_id ( $district, 3 ),
					'address' => $address,
					'address_name' => $address_name,
					'sign_building' => $sign_building,
					'sex' => $sex,
					'address_tag' => $address_tag,
					'mobile' => $mobile
			);
			$address = $province . $city . $district . $address;
			$lnglat = get_lng_lat ( $address );
			if ($lnglat) {
				$longitude = $lnglat ['lng'];
				$latitude = $lnglat ['lat'];
			} else {
				$longitude = '';
				$latitude = '';
			}
			$data ['longitude'] = $longitude;
			$data ['latitude'] = $latitude;
			$res = $useraddress->where($where)->save ( $data );
			$result['error_code'] = 100;
			if ($res!==false) {
				returnApiSuccess($result,'修改收货地址成功');
			} else {
				returnApiError ( '修改收获地址失败' );
			}
			
		} else {
			returnApiError ( '查无此收货地址' );
		}
		
		
// 		$count = D ( 'Useraddress' )->get_addresscount ( $where );
// 		if ($count == 1) {
// 			$data = array (
// 					'user_id' => $uid,
// 					'address_id' => $address_id,
// 					'consignee' => $consignee,
// 					'country' => 1,
// 					'province' => D ( 'Useraddress' )->get_address_id ( $province, 1 ),
// 					'city' => D ( 'Useraddress' )->get_address_id ( $city, 2 ),
// 					'district' => D ( 'Useraddress' )->get_address_id ( $district, 3 ),
// 					'address' => $address,
// 					'sex' => $sex,
// 					'mobile' => $mobile 
// 			);
// 			$address = $province . $city . $district . $address;
// 			$lnglat = get_lng_lat ( $address );
// 			if ($lnglat) {
// 				$longitude = $lnglat ['lng'];
// 				$latitude = $lnglat ['lat'];
// 			} else {
// 				$longitude = '';
// 				$latitude = '';
// 			}
// 			$data ['longitude'] = $longitude;
// 			$data ['latitude'] = $latitude;
// 			$id = D ( 'Useraddress' )->edit_address ( $data );
// 			if ($id) {
// 				returnApiSuccess ( array (
// 						'id' => $id 
// 				) );
// 			} else {
// 				returnApiError ( '修改收获地址失败' );
// 			}
// 		} else {
// 			returnApiError ( '查无此收货地址' );
// 		}
	}
		
		/*
	 * 收货地址详情
	 *
	 */
	public function my_address_details() {
		parent::check_uid ();
		$uid = $this->uid;
		$address_id = I ( 'post.address_id', 'intval',0 );
		if (!$address_id) {
			returnApiError ( '参数错误' );
		}
		$useraddress_mod = D ( 'Useraddress' );
		$res = $useraddress_mod->get_address_details ( array (
					'address_id' => $address_id,
					'user_id' => $uid 
			) );
			if ($res) {
				returnApiSuccess ($res);
			} else {
				returnApiError ( '此收货地址不存在' );
			}
	}
    
    /*
     * 设定默认收货地址
     * */
    public function my_address_set(){
        parent::check_uid();
        $uid=$this->uid;
       $address_id=I('post.address_id','intval');
       if(!isset($address_id)){
       		returnApiError('参数错误');
       }
       $where['user_id']=$uid;
       $where['address_id']=$address_id;
       $useraddress_mod=D('Useraddress');
       $count=$useraddress_mod->get_addresscount($where);
       if($count==1){
       	   $id=$useraddress_mod->set_address_dafult($uid,$address_id);
       	   if($id){
       	   	   returnApiSuccess(array('id'=>$id));
       	   }else{
       	   	  returnApiError('设置默认收获地址失败');
       	   }
       }else{
       	  returnApiError('查无此收货地址');
       }
    }
    
    /*
     * 获取默认收货地址
     * */
    public function my_address_default(){
    	 parent::check_uid();
    	 $uid=$this->uid;
    	$res=D('Useraddress')->get_address_dafult($uid);
    	if($res){
    		returnApiSuccess($res);
    	}else{
    		returnApiError('查无此默认收货地址');
    	}
    }
    
    /*
     * 删除收货地址
     * */
    
    public function my_address_del(){
        parent::check_uid();
        $uid=$this->uid;
       $address_id=I('post.address_id','intval');
       if(!isset($address_id)){
       		returnApiError('参数错误');
       }
       $where['user_id']=$uid;
       $where['address_id']=$address_id;
       $useraddress_mod=D('Useraddress');
       $count=$useraddress_mod->get_addresscount($where);
       if($count==1){
       	   $id=$useraddress_mod->del_address($uid,$address_id);
       	   if($id){
       	   	 returnApiSuccess(array('id'=>$id));
       	   }else{
       	   	   returnApiError('删除收货地址失败');
       	   }
       }else{
       	  returnApiError('查无此收货地址');
       }
    }
    
    /*
     * 用户定位
     * */
    public function users_location(){
    	$location=I('post.location',0,'intval');
    	if($location==0){
    		parent::check_uid();
    		$uid=$this->uid;
    	}
        $long=I('post.long');
        $lati=I('post.lati');
        $res=D('Users')->set_users_location($uid,$location,$long,$lati);

        if($res['return']){
            returnApiSuccess(array('distribution'=>$res));
        }else{
            returnApiError('用户定位失败',$res);
        }
    }
	/*
 * 用户定位--根据地址内容
 * */
	public function users_location_by_address(){
		//parent::check_uid();
		$address=I('post.address');


//		$data=address_to_gps(str_ireplace(' ','',$address));
//		$latitude=$data['latitude'];
//		$longitude=$data['longitude'];
//		$arr=D('Users')->get_distribution_by_gps($longitude, $latitude);


		$date = address_to_gps($address);
		//dump($address);
		//dump($date);
		$res=D('Users')->set_users_location(null,1,$date['longitude'],$date['latitude']);
		if($res['return']){
			returnApiSuccess(array('distribution'=>$res));
		}else{
			returnApiError('用户定位失败',$res);
		}
	}
    /*
     * 获取用户收货地址转经纬度
     * */
    
    public function address_conversion(){
        parent::check_uid();
        $uid=$this->uid;
        $address=I('post.address');
        $res=D('Users')->get_address_conversion($address);
        returnApiSuccess(array('longlati'=>$res));
    }
    
    /*
     * 根据订单收货地址获取对应仓库ID
     * */
    public function order_shipping_address_get_wid(){
        $order_id=I('post.order_id');
        $user_id=I('post.user_id');
        if(empty($order_id) || empty($user_id)){
            returnApiError('参数错误');
        }
        $order=M('order_info')->where(array('order_id'=>$order_id,'user_id'=>$user_id))->find();
        if($order){
            $country = M('region')->where('region_id ='.$order['country'])->getField('region_name');
            $province = M('region')->where('region_id ='.$order['province'])->getField('region_name');
            $city = M('region')->where('region_id ='.$order['city'])->getField('region_name');
            $district = M('region')->where('region_id ='.$order['district'])->getField('region_name');
            $all_address = $country.$province.$city.$district.$order['address'];
            $gps=address_to_gps($all_address);
            $res=D('Users')->get_distribution_by_gps($gps['longitude'],$gps['latitude']);
            returnApiSuccess(array('data'=>$res));
        }else{
            returnApiError('此订单不存在');
        }
    }
    
   /*根据ID获取区域对应名称*/
    private  function get_region_name($id){
    	$res=M('region')->where(array('region_id'=>$id))->find();
    	return $res['region_name'];
    }
    
    
    
    
}
